﻿using System;
using System.Collections;
using System.Collections.Generic;

public static class GameDebug
{
    public static void LogException(Exception e)
    {
        LogError(e.Message + "\n" + e.StackTrace);
    }

    public static void Log(object log, string battleId = "")
    {
        // 服务端 
#if NETCOREAPP
        WriteLog2File((string)log, battleId);

        if (string.IsNullOrEmpty(battleId))
        { 
            System.Console.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss-fff: ") + log);
        }
		// 客户端
#else

#if !RELEASE
        UnityEngine.Debug.Log(log);
#endif

#endif
    }

    public static void LogNotes(object obj)
    {
    }

    public static void LogShowy(object log, string battleId = "")
    {
        // 服务端 
#if NETCOREAPP
        WriteLog2File((string)log, battleId);

        if (string.IsNullOrEmpty(battleId))
        { 
            System.Console.ForegroundColor = System.ConsoleColor.Yellow;
            System.Console.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss-fff: ") + log);
            System.Console.ForegroundColor = (System.ConsoleColor.Gray);
        }

        // 客户端
#elif !RELEASE

        UnityEngine.Debug.Log("<color=yellow>" + log + "</color>");

#endif
    }


    public static void LogError(object log, string battleId = "")
    {
        // 服务端 
#if NETCOREAPP
        WriteLog2File((string)log, battleId);

        if (string.IsNullOrEmpty(battleId))
        {
            System.Console.ForegroundColor = System.ConsoleColor.Red;
            System.Console.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss-fff: ") + log);
            System.Console.ForegroundColor = (System.ConsoleColor.Gray);
        }
		// 客户端
//#elif  !RELEASE
#else
        UnityEngine.Debug.LogError(log);
#endif
    }

#if NETCOREAPP
    static AtomLock atomLock = new AtomLock();

    static void WriteLog2File(string log,string battleId)
    {
       if (string.IsNullOrEmpty(battleId))
       {
            return;
            atomLock.DoAction(() =>
            {
                Log2File.WriteLogFile(log);
            });
       }
       else
       {
            atomLock.DoAction(() =>
            {
               string fname = $"{DateTime.Now.Year}_{DateTime.Now.Month}_{DateTime.Now.Day}_{battleId}.txt";
               Log2File.WriteLogFileWithName(fname,log);

            });
       }
    }
#endif

    public static string GetListStr<T>(this List<T> arr)
    {
        return GetArrStr(arr.ToArray(), false);
    }

    public static string GetListStr<T>(this List<T> arr, bool newline)
    {
        return GetArrStr(arr.ToArray(), newline);
    }

    public static string GetArrStr<T>(this T[] arr, bool newline = false)
    {
        string str = "[";
        foreach (var e in arr)
        {
            if (e == null)
                str += "null";
            else
                str += e.ToString();

            if (newline)
                str += "\n";
            else
                str += ", ";
        }

        str += "]";
        return str;
    }

    public static string GetArrStr(this float[,] arr)
    {
        string str = "";
        for (int i = 0; i < arr.GetLength(0); i++)
        {
            for (int j = 0; j < arr.GetLength(1); j++)
            {
                str += arr[i, j] + ",";
            }

            str += "\n";
        }

        return str;
    }

    public static string GetDictStr<T1, T2>(this Dictionary<T1, T2> arr,bool newline = false)
    {
        string str = "";
        foreach (var e in arr)
        {
            str += $"[{e.Key}:{e.Value}]"; // e.ToString() + ", ";
            if (newline)
            {
                str += "\n";
            }
            else
            {
                str += ",";
            }
        }

        return str;
    }
  
}